package com.bandit.config;

import com.bandit.utils.Constants;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.*;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;


@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Value("${bandit.upload}")
    private String uploadPath;

    @Resource(name="thymeleafViewResolver")
    private ThymeleafViewResolver thymeleafViewResolver;

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {//thymeleaf全局变量
        if (thymeleafViewResolver != null) {
            Map<String, Object> vars = new HashMap<>();
            vars.put("MSG_TYPE_JOBS", Constants.MSG_TYPE_JOBS);
            vars.put("MSG_TYPE_NEWS", Constants.MSG_TYPE_NEWS);
            vars.put("MSG_TYPE_NOTICES", Constants.MSG_TYPE_NOTICES);
            vars.put("MSG_TYPE_MAJOR", Constants.MSG_TYPE_MAJOR);
            thymeleafViewResolver.setStaticVariables(vars);
        }
        WebMvcConfigurer.super.configureViewResolvers(registry);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //jar包方式映射
        registry.addResourceHandler("/upload/**").addResourceLocations(uploadPath);
    }
}
